/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package deadlocksim.backend.io;
import deadlocksim.*;
import deadlocksim.backend.process.*;
import deadlocksim.backend.resources.*;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.BufferedReader;
import java.io.PrintWriter;
import java.io.IOException;
import java.util.*;



/**
 *
 * @author romilian
 */
public class ResourceFileLoader {
    
    //public static String inFileName = "proc-exec-in.txt";
    public static String inFileName;
    //"C:/Documents and Settings/Administrator/My Documents/NetBeansProjects/DeadlockSim/proc-exec-in.txt";
    //public static String outFileName = "proc-exec-out.txt";
    
    //public static void main(String[] args) throws IOException {
    public ResourceFileLoader(String inFileName) {
        this.inFileName = inFileName;
    }
    
      public void createResources(DeadlockSim mySim) throws IOException {
          //LinkedList<ProcessResource> resourceList = new LinkedList<ProcessResource>();
        BufferedReader inputStream = null;
        PrintWriter outputStream = null;
        //DeadlockSim mySim = new DeadlockSim();
                
        try {
            inputStream = 
                new BufferedReader(new FileReader(inFileName));
            //outputStream = 
            //    new PrintWriter(new FileWriter(outFileName));
                
            
            String l;
            while ((l = inputStream.readLine()) != null) {
                
                String[] words = l.trim().toUpperCase().split("\\s+"); // Separated by "whitespace"
                
                if(words.length == 0)
                {
                    emptyLineError();
                    return;
                }

                //if(words[0].equals(COMPUTE))
                //{
                    if(words.length < 2 || words[1] == null)
                    {
                        resourceFormatError();
                        return;
                    }
                    String resourceTypeString = words[0];
                    String resourceCountString = words[1];
                    
                    Integer resourceType = null;
                    Integer resourceCount = null;
                    try
                    {
                        resourceType = new Integer(resourceTypeString);
                        resourceCount = new Integer(resourceCountString);
                    }
                    catch(Exception e)
                    {
                        resourceFormatError();
                    }
                    for(int i = 0; i < resourceCount; i++)
                            mySim.createResource(resourceType);
                //}
                             
            }
                
                //outputStream.println(l);
            
        } finally {
            if (inputStream != null) {
                inputStream.close();
            }
            if (outputStream != null) {
                outputStream.close();
            }
            
        }
    }

      public HashMap<Integer, Integer> createResourceList() throws IOException {
        BufferedReader inputStream = null;
        PrintWriter outputStream = null;
        HashMap<Integer, Integer> resourceList = null;
                        
        try {
            inputStream = 
                new BufferedReader(new FileReader(inFileName));
            //outputStream = 
            //    new PrintWriter(new FileWriter(outFileName));
                
            
            String l;
            while ((l = inputStream.readLine()) != null) {
                
                String[] words = l.trim().toUpperCase().split("\\s+"); // Separated by "whitespace"
                
                if(words.length == 0)
                {
                    emptyLineError();
                    return null;
                }

                //if(words[0].equals(COMPUTE))
                //{
                    if(words.length < 2 || words[1] == null)
                    {
                        resourceFormatError();
                        return null;
                    }
                    String resourceTypeString = words[0];
                                       
                    Integer resourceType;
                    try
                    {
                        resourceType = new Integer(resourceTypeString);
                        resourceList.put(resourceType, 0);
                        
                    }
                    catch(Exception e)
                    {
                        resourceFormatError();
                    }
                //}
                             
            }
                
                //outputStream.println(l);
            
        } finally {
            if (inputStream != null) {
                inputStream.close();
            }
            if (outputStream != null) {
                outputStream.close();
            }
            return resourceList;
        }
    }      
      
      
      
      
      
      
      
    private static void emptyLineError() {
        throw new UnsupportedOperationException("Not yet implemented");
    }

    private static void resourceFormatError() {
        throw new UnsupportedOperationException("Not yet implemented");
    }

}
